<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
import resize from "../../dashboard/mixins/resize";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    pieData: {
      type: Array,
      default: function () {
        return [
          { value: 40, name: '工厂' },
          { value: 38, name: '医院' },
          { value: 32, name: '办公楼' },
          { value: 30, name: '酒店' },
          { value: 28, name: '学校' },
        ];
      },
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  watch: {
    'pieData': {
      handler() {
        this.$nextTick(() => {
          if (!this.chart) {
            return;
          }
          this.chart.dispose();
          this.chart = null;
          this.initChart();
        });
      }
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el);
      this.chart.setOption({
        legend: {
          orient: 'vertical',
          x:'right',//可设定图例在左、右、居中
          y:'center',//可设定图例在上、下、居中
          padding:[0,100,0,0],//可设定图例[距上方距离，距右方距离，距下方距离，距左方距离]
          data: ['工厂','医院','办公楼','酒店','学校'],
          textStyle: {
            color: "#ffffff",//文字的颜色。
            fontStyle: "normal",//文字字体的风格。
            fontWeight: "normal",//文字字体的粗细。 'normal' 'bold'  'bolder' 'lighter'  100 | 200 | 300 | 400...
            fontFamily: "sans-serif",//文字的字体系列。
            fontSize: 14,//文字的字体大小。
            lineHeight: 20,//行高。
          },
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        series: [
          {
            name: '项目类型占比',
            type: 'pie',
            radius: [20, 100],
            center: ['25%','50%'],
            roseType: 'area',
            itemStyle: {
              borderRadius: 5
            },
            label: {
              show: false,
            },
            // data: [
            //   { value: 1, name: '工厂' },
            //   { value: 2, name: '医院' },
            //   { value: 3, name: '办公楼' },
            //   { value: 4, name: '酒店' },
            //   { value: 5, name: '学校' },
            // ],
            data:this.pieData,
            animationDuration: 1500,
          }
        ]
      });
    },
  },
};
</script>

<style scoped lang="scss">

</style>
